/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tamnhat.entity;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import telsoft.admin.util.AppServer;
import telsoft.sql.Database;

/**
 *
 * @author Administrator
 */
public class GameAndroid implements Serializable {

    private int itemId;
    private String titleVn;
    private String titleEn;
    private String descriptionVn;
    private String preview;
    private String downloadLink;
    private String handset;
    private int downloadCount;

    public GameAndroid() {
    }

    public int getDownloadCount() {
        return downloadCount;
    }

    public void setDownloadCount(int downloadCount) {
        this.downloadCount = downloadCount;
    }

    public int getItemId() {
        return itemId;
    }

    public void setItemId(int itemId) {
        this.itemId = itemId;
    }

    public String getTitleVn() {
        return titleVn;
    }

    public void setTitleVn(String titleVn) {
        this.titleVn = titleVn;
    }

    public String getDescriptionVn() {
        return descriptionVn;
    }

    public void setDescriptionVn(String description) {
        this.descriptionVn = description;
    }

    public String getPreview() {
        return preview;
    }

    public void setPreview(String preview) {
        this.preview = preview;
    }

    public String getDownloadLink() {
        return downloadLink;
    }

    public void setDownloadLink(String downloadLink) {
        this.downloadLink = downloadLink;
    }

    public String getHandset() {
        return handset;
    }

    public void setHandset(String handset) {
        this.handset = handset;
    }

    public String getTitleEn() {
        return titleEn;
    }

    public void setTitleEn(String titleEn) {
        this.titleEn = titleEn;
    }

    //lay ra so cac game android trong database
    public static int getNoGame() {
//        Connection conn = ConnOrac.getConn();
//        try {
//            Statement st = conn.createStatement();
//            ResultSet rs = st.executeQuery("select count(*) from md_items where media_type = 'game-android'");
//            while (rs.next()) {
//                int i = Integer.parseInt(rs.getString(1));
//                return i;
//            }
//            conn.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//            try {
//                conn.close();                
//            } catch (SQLException e1) {
//                e1.printStackTrace();
//            }
//            return 0;
//        }
        return 0;
    }
    // check xem so moi nhap vao da ton tai trong day chua

    public static boolean checkListNumber(ArrayList<Integer> list, int x) {
        if (list.size() == 0) {
            return false;
        }
        for (int i = 0; i < list.size(); i++) {
            if (x == list.get(i)) {
                return true;
            }
        }
        return false;
    }
    //lay ra cac so ngau nhien

    public static ArrayList<Integer> getRandomListNumber(int size) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        int noGame = getNoGame();
        if (size >= noGame) {
            for (int i = 0; i < noGame; i++) {
                list.add(i + 1);
            }
            return list;
        } else {
            int i = 0;
            int check = 0;
            while (i < size) {
                check = (int) (Math.random() * noGame);
                if (GameAndroid.checkListNumber(list, check) == false) {
                    i++;
                    list.add(check);
                }
            }
            return list;
        }
    }

    //lay ra danh sach id cua cac game co trong database
    public static ArrayList<Integer> getRandomListIdGame(int size) {
//        Connection conn = ConnOrac.getConn();
//        ArrayList<Integer> list = new ArrayList<Integer>();
//        ArrayList<Integer> list2 = GameAndroid.getRandomListNumber(size);
//        ArrayList<Integer> list3 = new ArrayList<Integer>();
//        try {
//            Statement st = conn.createStatement();
//            ResultSet rs = st.executeQuery("select * from  md_items where media_type = 'game-android'");
//            while (rs.next()) {
//                list.add(Integer.parseInt(rs.getString("ITEM_ID")));
//            }
//            if (list.size() <= 0) {
//                conn.close();
//                return list3;
//            } else {
//                if (list.size() == 1) {
//                    list3.add(list.get(0));
//                } else {
//                    for (int i = 0; i < list2.size(); i++) {
//                        System.out.println("==kiem tra");
//                        list3.add(list.get(list2.get(i)));
//                    }
//                }
//            }
//            conn.close();
//            return list3;
//        } catch (Exception e) {
//            e.printStackTrace();
//            try {
//                conn.close();
//            } catch (SQLException e1) {
//                e1.printStackTrace();
//            }
//        }
//
        return null;
    }
    //lay ra danh sach ngau nhien cac game android

    public static ArrayList<GameAndroid> getRandomListGame(int size) {
//        Connection conn = ConnOrac.getConn();
//        ArrayList<GameAndroid> listGame = new ArrayList<GameAndroid>();
//        ArrayList<Integer> listID = GameAndroid.getRandomListIdGame(size);
//
//        try {
//            if (listID.size() == 0) {
//                conn.close();
//                return listGame;
//            }
//            for (int i = 0; i < listID.size(); i++) {
//                Statement st = conn.createStatement();
//                ResultSet rs = st.executeQuery("select * from md_item_properties,md_items where md_items.media_type = 'game-android' and md_item_properties.item_id = md_items.item_id and md_items.item_id=" + listID.get(i) + " order by md_item_properties.item_id");
//                GameAndroid g = new GameAndroid();
//                g.setItemId(listID.get(i));
//                while (rs.next()) {
//                    if (rs.getString(3).equalsIgnoreCase("title.vn")) {
//                        g.setTitleVn(rs.getString(4));
//                    }
//                    if (rs.getString(3).equalsIgnoreCase("title.en")) {
//                        g.setTitleEn(rs.getString(4));
//                    }
//                    if (rs.getString(3).equalsIgnoreCase("description.vn")) {
//                        g.setDescriptionVn(rs.getString(4));
//                    }
//                    if (rs.getString(3).equalsIgnoreCase("preview")) {
//                        g.setPreview(rs.getString(4));
//                    }
//                    if (rs.getString(3).equalsIgnoreCase("handset")) {
//                        g.setHandset(rs.getString(4));
//                    }
//                    if (rs.getString(3).equalsIgnoreCase("downloadlink")) {
//                        g.setDownloadLink(rs.getString(4));
//                    }
//                }
//                listGame.add(g);
//            }
//            conn.close();
//            return listGame;
//        } catch (Exception e) {
//            e.printStackTrace();
//            try {
//                conn.close();
//            } catch (SQLException e1) {
//                e1.printStackTrace();
//            }
//        }
//        return listGame;
        return null;
    }
    //lay tat ca cac game trog DB

    public static ArrayList<GameAndroid> getAll() {
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        ArrayList<GameAndroid> listGame = new ArrayList<GameAndroid>();

        try {
            cn = AppServer.getConnection();
            st = cn.createStatement();
            rs = st.executeQuery("select * "
                    + "from md_item_properties,md_items "
                    + "where md_items.media_type = 'game-android' and md_item_properties.item_id = md_items.item_id "
                    + "order by md_item_properties.item_id");
            GameAndroid g = new GameAndroid();
            int i = 0;
            while (rs.next()) {
                i++;
                if (rs.getString(3).equalsIgnoreCase("title.vn")) {
                    g.setTitleVn(rs.getString(4));
                }
                if (rs.getString(3).equalsIgnoreCase("title.en")) {
                    g.setTitleEn(rs.getString(4));
                }
                if (rs.getString(3).equalsIgnoreCase("description.vn")) {
                    g.setDescriptionVn(rs.getString(4));
                }
                if (rs.getString(3).equalsIgnoreCase("preview")) {
                    g.setPreview(rs.getString(4));
                }
                if (rs.getString(3).equalsIgnoreCase("handset")) {
                    g.setHandset(rs.getString(4));
                }
                if (rs.getString(3).equalsIgnoreCase("downloadlink")) {
                    g.setItemId(Integer.parseInt(rs.getString("ITEM_ID")));
                    g.setDownloadLink(rs.getString(4));
                    g.setDownloadCount(Integer.parseInt(rs.getString(11)));
                }
                if (i == 9) {
                    listGame.add(g);
                    g = new GameAndroid();
                    i = 0;
                }
            }            
//            System.out.println("====size===" + listGame.size());
            return listGame;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Database.closeObject(rs);
            Database.closeObject(st);
            Database.closeObject(cn);
        }
        return listGame;
    }

    public static ArrayList<String> parseHandset(String input) {
        if (input != null) {
            String[] str = input.split(":");
            String[] str2;
            ArrayList<String> brand = new ArrayList<String>();
            ArrayList<String> model = new ArrayList<String>();
            int x = 0;
            for (int i = 0; i < str.length; i++) {
                if (i == 0) {
                    brand.add(str[i]);
                }
                str2 = str[i].split("\n");
                int len = str2.length;
                for (int j = 0; j < len; j++) {
                    if (j == (len - 1) && (len > 1)) {
                        x++;
                        if (str2[j] != null) {
                            brand.add(str2[j]);
                        }
                    }
                }
            }
            return brand;
        }
        ArrayList<String> list2 = new ArrayList<String>();
        list2.add("");
        return list2;
//        System.out.println("KET QUA CUOI CUNG: ");
//        for (int i = 0; i < brand.size(); i++) {
//            System.out.println("brand==" + brand.get(i));
//            System.out.println("model==" + model.get(i));
//            System.out.println("-----------------------------------------------------------");
//        }

    }

}
